package com.example.demo.test.ListComparable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        int i = this.name.compareTo(o.name);
        if (i ==0){
            i = this.age - o.age;
        }
        return i;
    }

    public static void main(String[] args) {
        Student student = new Student("李斯", 1);
        Student student2 = new Student("李斯", 5);
        Student student3 = new Student("李斯", 2);
        List<Student> students = new ArrayList<Student>();
        students.add(student);
        students.add(student2);
        students.add(student3);
//        Collections.sort(students);
//        for (Student s:students
//             ) {
//            System.out.println(s.name+","+s.age);
//        }



//        Collections.sort(students,(o1, o2) -> {
//            int i = o1.name.compareTo(o2.name);
//            if (i ==0){
//                i = o1.age - o2.age;
//            }
//            return i;
//        });
//        for (Student s:students
//        ) {
//            System.out.println(s.name+","+s.age);
//        }

        // 需要类中实现comparable接口
//        List<Student> collect = students.stream().sorted().collect(Collectors.toList());
//        for (Student s:collect
//        ) {
//            System.out.println(s.name+","+s.age);
//        }


        // 删除不需要的元素
        List<Student> collect2 = students.stream().filter(s -> s.age < 5).collect(Collectors.toList());
        for (Student s:collect2
        ) {
            System.out.println(s.name+","+s.age);
        }
    }


}
